﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SelectingStudents
{
    class SelectingStudents
    {
        static void Main(string[] args)
        {
            Student pesho = new Student("Petar", "Ivanov", 1241254, "088/234567", "pesho@abv.bg", new List<int>() { 2, 4, 5 }, 2);
            Student ivan = new Student("Ivan", "Kesiov", 535123, "088/345287", "ivan@gmail.bg", new List<int>() { 3, 2, 5 }, 2);
            Student kolio = new Student("Nikolai", "Konev", 46364, "088/256908", "nikito@abv.bg", new List<int>() { 6, 4, 3 }, 1);
            Student ana = new Student("Anelia", "Romanova", 141566, "02/123987", "sladurana@yahoo.bg", new List<int>() { 2, 4, 2 }, 2);
            Student ecio = new Student("Ecio", "Auditore", 3523065, "088/234789", "quickdeath@abv.bg", new List<int>() { 6, 4, 5 }, 4);
            Student tisho = new Student("Tihomir", "Mihailov", 56230634, "088/214287", "imwithstupid@abv.bg", new List<int>() { 2, 2, 2 }, 4);

            List<Student> students = new List<Student>() { pesho, ivan, kolio, ana, ecio, tisho };
            //var secondGroup =
            //    from hlape in students
            //    where hlape.GroupNumber == 2
            //    orderby hlape.FirstName
            //    select hlape;

            //foreach (var item in secondGroup)
            //{
            //    Console.WriteLine(item);
            //}
            //Console.WriteLine();

            //var extensionSecondGroup = students.Where(g => g.GroupNumber == 2).OrderBy(g => g.FirstName);

            //foreach (var item in extensionSecondGroup)
            //{
            //    Console.WriteLine(item);
            //}

            //var abvStudents =
            //    from hlape in students
            //    where hlape.Email.Contains("abv.bg")
            //    select hlape;

            //foreach (var item in abvStudents)
            //{
            //    Console.WriteLine(item);
            //}

            //Regex sofiaCode = new Regex("02");

            //var sofians =
            //    from hlape in students
            //    where sofiaCode.Match(hlape.Tel,0).ToString() != string.Empty
            //    select hlape;

            //foreach (var item in sofians)
            //{
            //    Console.WriteLine(item);
            //}

            //var withExcelent =
            //    from hlape in students
            //    where hlape.Marks.Contains(6)
            //    select new { name = hlape.FirstName, marks = hlape.Marks };

            //foreach (var item in withExcelent)
            //{
            //    Console.Write(item.name+" ");
            //    foreach (var mark in item.marks)
            //    {
            //        Console.Write(mark+" ");
            //    }
            //    Console.WriteLine();
            //}

            //var poorStudents = students.Where(hlape => hlape.Marks.Count(g => g == 2) == 2);

            //foreach (var item in poorStudents)
            //{
            //    Console.WriteLine(item);
            //}

            //var sixthYearStudent =
            //    from hlape in students
            //    where hlape.FN.ToString()[4] == '0' && hlape.FN.ToString()[5] == '6'
            //    select hlape;

            //foreach (var item in sixthYearStudent)
            //{
            //    Console.WriteLine(item);
            //}

            //pesho.Group = new Group(pesho.GroupNumber, "Mathematics");
            //ivan.Group=new Group(pesho.GroupNumber,"Literature");
            //kolio.Group = new Group(pesho.GroupNumber, "Mathematics");
            //ana.Group = new Group(pesho.GroupNumber, "Mathematics");
            //ecio.Group=new Group(pesho.GroupNumber,"Physics");
            //tisho.Group = new Group(pesho.GroupNumber, "Sport");

            //var groups = new[] 
            //{
            //    new Group(1, "Literature"),
            //    new Group(2, "Mathematics"),
            //    new Group(3, "Physics"),
            //    new Group(4, "Sport")
            //};

            //var mathStudents =
            //    from hlape in students
            //    join g in groups
            //    on hlape.GroupNumber equals g.GroupNumber
            //    where hlape.GroupNumber==2
            //    select hlape;

            //foreach (var item in mathStudents)
            //{
            //    Console.WriteLine(item);
            //}

            //var groupedStudents =
            //    from hlape in students
            //    group hlape by hlape.GroupNumber;

            //foreach (var item in groupedStudents)
            //{
            //    Console.WriteLine(item.Key);
            //    foreach (var student in item)
            //    {
            //        Console.WriteLine("     "+student.FirstName);
            //    }
            //    Console.WriteLine();
            //}

            var extensionGroupedStudents = students.GroupBy(g => g.GroupNumber);

            foreach (var item in extensionGroupedStudents)
            {
                Console.WriteLine(item.Key);
                foreach (var student in item)
                {
                    Console.WriteLine("     " + student.FirstName);
                }
                Console.WriteLine();
            }
        }
    }
}
